"""
    Controller for charge records viewing and editing.
"""

import turbogears
from turbogears.widgets import TextField
from mnemoply import model
from mnemoply.widgets import DecimalValidator, DictionaryField
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController
from mnemoply.mass_actions import MassAction, MassActionForm

class MAChangePayedHours(MassAction):
    target_class = model.ChargeRecord
    name = "change_hours_for_register"
    title = _("Change register")
    form = MassActionForm(name=name,
        fields=[
            TextField(name = "hours",
                      label=_("Hours for register"),
                      validator=DecimalValidator(not_empty=False)),
        ]
    )

    def call(self, objs, hours):
        for cr in objs:
            cr.payedHours = hours

class MAChangeRealHours(MassAction):
    target_class = model.ChargeRecord
    name = "change_real_hours"
    title = _("Change real hours")
    form = MassActionForm(name=name,
        fields=[
            TextField(name = "hours",
                      label=_("Real hours"),
                      validator=DecimalValidator(not_empty=False)),
            TextField(
                name = "comment",
                )
        ]
    )

    def call(self, objs, hours, comment):
        for cr in objs:
            cr.realHours = hours
            cr.comment = cr.comment+' '+ comment  if cr.comment else comment

class MAChangeDiscont(MassAction):
    target_class = model.ChargeRecord
    name = "change_discont"
    title = _("Change discont")
    form = MassActionForm(
        name=name,
        fields=[
            DictionaryField(
                name="discont_id",
                model_object_name='Discont',
                label=_("Discont"),
                ),
            TextField(
                name = "comment",
                )
            ]
        )

    def call(self, objs, discont_id,comment):
        discont = model.Discont.get_by_null_id(discont_id)
        for cr in objs:
            cr.discont = discont
            cr.comment = cr.comment+' '+ comment  if cr.comment else comment

class ChargeRecordController(LookupController, CRUDController):
    model_object = model.ChargeRecord
    lookup_columns = [
        TableCol(name='groups', caption=_('Group'), size=14),
        TableCol(name='student', caption=_('Student')),
        TableCol(name='billing_year', caption=_('Year'), size=4),
        TableCol(name='billing_month', caption=_('Month'), size=4),
        TableCol(name='planned_hours', caption=_('Planned h.'), size=4),
        TableCol(name='real_hours', caption=_('Real h.'), size=4),
        TableCol(name='payed_hours', caption=_('Payed h.'), size=4),
        TableCol(name='tariff', caption=_('Tariff'), size=4),
        TableCol(name='amount', caption=_('Amount'), size=4),
        TableCol(name='cache_balance', caption=_('Balance'), size=4),
        TableCol(name='discont', caption=_("Discont"), hidden=True),
        TableCol(name='prev_discont', caption=_("Previous discont"), hidden=True),
        TableCol(name='comment', caption=_('Comment'), hidden=True),
    ]
    view_fields = [
        'billingYear', 'billingMonth',
        'studentAgreementID',
        'studentID',
        'plannedHours', 'realHours',
        'payedHours',
        'tariff', 'discontID','comment'
    ]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource("charge_record_lookup"),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
            mass_actions_class=self.model_object,
        )
        CRUDController.__init__(self, model_object=self.model_object)

    def fill_lookup_data_defaults(self, data):
        is_parent_filled = LookupController.fill_lookup_data_defaults(self, data)
        if not is_parent_filled:
            office = turbogears.identity.current.user.office.name
            args = {'flt_office_str':office,
                    'column':['groups',
                              'student',
                              'billing_year',
                              'billing_month',
                              'planned_hours',
                              'real_hours',
                              'tariff',
                              'amount'
                              ]
                    }
            data.update(args)

exposed = ChargeRecordController()
